package com.bango.nokia.java;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.Vector;

import org.json.me.JSONArray;
import org.json.me.JSONObject;

import com.twmacinta.util.MD5;

class BangoStatic
{
	static boolean IsStringNullOrEmpty(String input)
	{
		if ((input == null) || (input.equals("")) || (input.equalsIgnoreCase("null")))
		{
			return true;
		}
		return false;
	}

	static String GetProperty(String propertyName)
	{
		return GetProperty(propertyName, "");
	}

	static String GetProperty(String propertyName, String defaultValue)
	{
		String rtnValue = defaultValue;
		try
		{
			rtnValue = System.getProperty(propertyName);
			if (IsStringNullOrEmpty(rtnValue))
			{
				rtnValue = defaultValue;
			}
		}
		catch (Exception e)
		{
			rtnValue = defaultValue;
		}
		return rtnValue;
	}

	static Date GetUTCDate()
	{
		Date dt = new Date();
		return GetUTCDate(dt);
	}

	static Date GetUTCDate(Date dt)
	{
		TimeZone tz = TimeZone.getDefault();

		long millis = dt.getTime();
		long offset = tz.getRawOffset();

		dt = new Date(millis - offset);

		return dt;
	}

	static String ConnectionType(int connectionType)
	{
		String returnValue = "OTHER";

		switch (connectionType) {
			case BangoConstants.WIFI_CONNECTION:
				returnValue = "WIFI";
				break;
			case BangoConstants.OPERATOR_CONNECTION:
				returnValue = "OPERATOR";
				break;
			default:
				returnValue = "OTHER";
				break;
		}

		return returnValue;
	}

	/**
	 * Gets operator connection type
	 * 
	 * @return The operator connection type
	 */
	static String GetOperatorConnectionType()
	{

		String str1, str2;

		str1 = BangoStatic.GetProperty(BangoConstants.ACCESS_NETWORK_ACCESS_TYPE);
		str2 = BangoStatic.GetProperty(BangoConstants.ACCESS_NETWORK_OPERATOR_TYPE);

		if (str1.equalsIgnoreCase("pd"))
		{
			return "GPRS";
		}
		else if (str1.equalsIgnoreCase("pd.EDGE"))
		{
			return "EDGE";
		}
		else if (str1.equalsIgnoreCase("pd.3G"))
		{
			return "HSPA";
		}
		else if (str1.equalsIgnoreCase("pd.HSDPA"))
		{
			return "HSDPA";
		}
		else if (str2.equalsIgnoreCase("GPRS"))
		{
			return "GPRS";
		}
		else if (str2.equalsIgnoreCase("EGPRS"))
		{
			return "EDGE";
		}
		else if (str2.indexOf("UMTS") >= 0)
		{
			return "UMTS";
		}

		return "OTHER";
	}

	static String GetUserAgent()
	{
		StringBuffer agentBuffer = new StringBuffer();

		agentBuffer.append("Mozilla/5.0 (");
		agentBuffer.append(GetOSName() + "/" + GetOSVersion() + "; ");
		if (BangoSymbianDeviceDetails.PlatformName() != "")
		{
			agentBuffer.append(BangoSymbianDeviceDetails.PlatformName());
			if (BangoSymbianDeviceDetails.PlatformVersion() != "")
			{
				agentBuffer.append("/" + BangoSymbianDeviceDetails.PlatformVersion());
			}
			agentBuffer.append(" ");
		}

		agentBuffer.append(BangoSymbianDeviceDetails.DeviceMake() + BangoSymbianDeviceDetails.DeviceModel());
		if (BangoSymbianDeviceDetails.ModelVersion() != "")
		{
			agentBuffer.append("-" + BangoSymbianDeviceDetails.ModelVersion());
		}
		if (BangoSymbianDeviceDetails.FirmwareVersion() != "")
		{
			agentBuffer.append("/" + BangoSymbianDeviceDetails.FirmwareVersion());
		}

		agentBuffer.append("; ");

		agentBuffer.append(" Profile/" + BangoSymbianDeviceDetails.Profile());
		agentBuffer.append(" Configuration/" + BangoSymbianDeviceDetails.Configuration());
		agentBuffer.append("; ");
		agentBuffer.append(") ");

		if (BangoSymbianDeviceDetails.JavaRuntimeVersion() != "")
		{
			agentBuffer.append("Java Runtime/" + BangoSymbianDeviceDetails.JavaRuntimeVersion() + "; ");
		}

		return agentBuffer.toString();
	}

	static String GetUserID()
	{
		String idString = BangoStatic.GetProperty(BangoConstants.ACCESS_IMEI_NUMBER);

		MD5 md5encoder = new MD5(idString);
		return md5encoder.asHex();
	}

	static String GetOSName()
	{
		return GetProperty(BangoConstants.ACCESS_OS_NAME, BangoConstants.UNKNOWN_INFORMATION);
	}

	static String GetOSVersion()
	{
		return GetProperty(BangoConstants.ACCESS_OS_VERSION);
	}

	static String GetJSONString(BangoProfile profile, Vector eventsToPost)
	{
		try
		{
			JSONObject postJSONObject;
			JSONArray postEvents;
			postJSONObject = profile.GetDeviceProfile();
			postEvents = new JSONArray();
			for (int appEventCounter = 0; appEventCounter < eventsToPost.size(); appEventCounter++)
			{
				if (!((BangoEvent) eventsToPost.elementAt(appEventCounter)).EventPosted())
				{
					postEvents.put(((BangoEvent) eventsToPost.elementAt(appEventCounter)).GetEventDetails());
				}
			}

			postJSONObject.put(BangoFieldNames.POST_EVENTS, postEvents);

			return postJSONObject.toString();
		}
		catch (Exception ex)
		{

		}

		return "";
	}

	static String GetDateString(Date inDate)
	{
		// Date Format = "yyyy-MM-dd'T'HH:mm:ss.SSS";

		Calendar clndr = Calendar.getInstance();
		clndr.setTime(inDate);
		int mth;

		String year = BangoStatic.PadLeft(Integer.toString(clndr.get(Calendar.YEAR)), '0', 4);

		// Months go from 0 - 11
		mth = clndr.get(Calendar.MONTH) + 1;
		String month = BangoStatic.PadLeft(Integer.toString(mth), '0', 2);

		String day = BangoStatic.PadLeft(Integer.toString(clndr.get(Calendar.DATE)), '0', 2);
		String hour = BangoStatic.PadLeft(Integer.toString(clndr.get(Calendar.HOUR_OF_DAY)), '0', 2);
		String minute = BangoStatic.PadLeft(Integer.toString(clndr.get(Calendar.MINUTE)), '0', 2);
		String second = BangoStatic.PadLeft(Integer.toString(clndr.get(Calendar.SECOND)), '0', 2);
		String millis = BangoStatic.PadLeft(Integer.toString(clndr.get(Calendar.MILLISECOND)), '0', 3);

		String rtnValue = year + "-" + month + "-" + day + "T" + hour + ":" + minute + ":" + second + "." + millis;

		return rtnValue;

	}

	static String GetCountryCode()
	{
		String out = "";

		out = BangoStatic.GetProperty(BangoConstants.ACCESS_COUNTRY_CODE_ITU);		
		if (IsStringNullOrEmpty(out))
		{
			out = BangoStatic.GetProperty("phone.mcc");
		}
		if (IsStringNullOrEmpty(out))
		{
			out = BangoStatic.GetProperty("com.sonyericsson.net.mcc");
		}
		if (IsStringNullOrEmpty(out))
		{
			out = getIMSI().equals("") ? "" : getIMSI().substring(0, 3);
		}
		if (IsStringNullOrEmpty(out))
		{
			out = BangoStatic.GetProperty("mcc");
		}

		return out == null ? "" : out;
	}

	static String getIMSI()
	{
		String out = "";
		out = BangoStatic.GetProperty("IMSI");
		if (IsStringNullOrEmpty(out))
		{
			out = BangoStatic.GetProperty("phone.imsi");
		}
		if (IsStringNullOrEmpty(out))
		{
			out = BangoStatic.GetProperty("com.nokia.mid.mobinfo.IMSI");
		}
		if (IsStringNullOrEmpty(out))
		{
			out = BangoStatic.GetProperty("com.nokia.mid.imsi");
		}
		if (IsStringNullOrEmpty(out))
		{
			out = BangoStatic.GetProperty("imsi");
		}
		return out == null ? "" : out;
	}

	static String GetCountryISOFromITU()
	{

		String countryITU = BangoStatic.GetProperty(BangoConstants.ACCESS_COUNTRY_CODE_ITU);
		int intITU;
		String rtnStr;

		try
		{
			intITU = Integer.parseInt(countryITU);
		}
		catch (NumberFormatException ex)
		{
			intITU = -1;
		}

		if (intITU <= 0)
		{
			rtnStr = countryITU;
		}
		else
		{
			switch (intITU) {
				case 202:
					rtnStr = "GRC";
					break;
				case 204:
					rtnStr = "NLD";
					break;
				case 206:
					rtnStr = "BEL";
					break;
				case 208:
					rtnStr = "FRA";
					break;
				case 212:
					rtnStr = "MCO";
					break;
				case 213:
					rtnStr = "AND";
					break;
				case 214:
					rtnStr = "ESP";
					break;
				case 216:
					rtnStr = "HUN";
					break;
				case 218:
					rtnStr = "BIH";
					break;
				case 219:
					rtnStr = "HRV";
					break;
				case 220:
					rtnStr = "SRB";
					break;
				case 222:
					rtnStr = "ITA";
					break;
				case 225:
					rtnStr = "VAT";
					break;
				case 226:
					rtnStr = "ROM";
					break;
				case 228:
					rtnStr = "CHE";
					break;
				case 230:
					rtnStr = "CZE";
					break;
				case 231:
					rtnStr = "SVK";
					break;
				case 232:
					rtnStr = "AUT";
					break;
				case 234:
				case 235:
					rtnStr = "GBR";
					break;
				case 238:
					rtnStr = "DNK";
					break;
				case 240:
					rtnStr = "SWE";
					break;
				case 242:
					rtnStr = "NOR";
					break;
				case 244:
					rtnStr = "FIN";
					break;
				case 246:
					rtnStr = "LTU";
					break;
				case 247:
					rtnStr = "LVA";
					break;
				case 248:
					rtnStr = "EST";
					break;
				case 250:
					rtnStr = "RUS";
					break;
				case 255:
					rtnStr = "UKR";
					break;
				case 257:
					rtnStr = "BLR";
					break;
				case 259:
					rtnStr = "MDA";
					break;
				case 260:
					rtnStr = "POL";
					break;
				case 262:
					rtnStr = "DEU";
					break;
				case 266:
					rtnStr = "GIB";
					break;
				case 268:
					rtnStr = "PRT";
					break;
				case 270:
					rtnStr = "LUX";
					break;
				case 272:
					rtnStr = "IRL";
					break;
				case 274:
					rtnStr = "ISL";
					break;
				case 276:
					rtnStr = "ALB";
					break;
				case 278:
					rtnStr = "MLT";
					break;
				case 280:
					rtnStr = "CYP";
					break;
				case 282:
					rtnStr = "GEO";
					break;
				case 283:
					rtnStr = "ARM";
					break;
				case 284:
					rtnStr = "BGR";
					break;
				case 286:
					rtnStr = "TUR";
					break;
				case 288:
					rtnStr = "FRO";
					break;
				case 290:
					rtnStr = "GRL";
					break;
				case 292:
					rtnStr = "SMR";
					break;
				case 293:
					rtnStr = "SVN";
					break;
				case 294:
					rtnStr = "MKD";
					break;
				case 295:
					rtnStr = "LIE";
					break;
				case 297:
					rtnStr = "ME";
					break;
				case 302:
					rtnStr = "CAN";
					break;
				case 308:
					rtnStr = "SPM";
					break;
				case 310:
				case 311:
				case 312:
				case 313:
				case 314:
				case 315:
				case 316:
					rtnStr = "USA";
					break;
				case 330:
					rtnStr = "PRI";
					break;
				case 332:
					rtnStr = "VIR";
					break;
				case 334:
					rtnStr = "MEX";
					break;
				case 338:
					rtnStr = "JAM";
					break;
				// 340 represents GLP (Guadaloupe) and MTQ (Martinique) - at
				// present, returns Guadaloupe.
				case 340:
					rtnStr = "GLP";
					break;
				case 342:
					rtnStr = "BRB";
					break;
				case 344:
					rtnStr = "ATG";
					break;
				case 346:
					rtnStr = "CYM";
					break;
				case 348:
					rtnStr = "VGB";
					break;
				case 350:
					rtnStr = "BMU";
					break;
				case 352:
					rtnStr = "GRD";
					break;
				case 354:
					rtnStr = "MSR";
					break;
				case 356:
					rtnStr = "KNA";
					break;
				case 358:
					rtnStr = "LCA";
					break;
				case 360:
					rtnStr = "VCT";
					break;
				case 362:
					rtnStr = "ANT";
					break;
				case 363:
					rtnStr = "ABW";
					break;
				case 364:
					rtnStr = "BHS";
					break;
				case 365:
					rtnStr = "AIA";
					break;
				case 366:
					rtnStr = "DMA";
					break;
				case 368:
					rtnStr = "CUB";
					break;
				case 370:
					rtnStr = "DOM";
					break;
				case 372:
					rtnStr = "HTI";
					break;
				case 374:
					rtnStr = "TTO";
					break;
				case 376:
					rtnStr = "TCA";
					break;
				case 400:
					rtnStr = "AZE";
					break;
				case 401:
					rtnStr = "KAZ";
					break;
				case 402:
					rtnStr = "BTN";
					break;
				case 404:
					rtnStr = "IND";
					break;
				case 405:
				case 406:
					rtnStr = "IN";
					break;
				case 410:
					rtnStr = "PAK";
					break;
				case 412:
					rtnStr = "AFG";
					break;
				case 413:
					rtnStr = "LKA";
					break;
				case 414:
					rtnStr = "MMR";
					break;
				case 415:
					rtnStr = "LBN";
					break;
				case 416:
					rtnStr = "JOR";
					break;
				case 417:
					rtnStr = "SYR";
					break;
				case 418:
					rtnStr = "IRQ";
					break;
				case 419:
					rtnStr = "KWT";
					break;
				case 420:
					rtnStr = "SAU";
					break;
				case 421:
					rtnStr = "YEM";
					break;
				case 422:
					rtnStr = "OMN";
					break;
				case 423:
					rtnStr = "PS";
					break;
				case 424:
					rtnStr = "ARE";
					break;
				case 425:
					rtnStr = "ISR";
					break;
				case 426:
					rtnStr = "BHR";
					break;
				case 427:
					rtnStr = "QAT";
					break;
				case 428:
					rtnStr = "MNG";
					break;
				case 429:
					rtnStr = "NPL";
					break;
				case 430:
				case 431:
					rtnStr = "ARE";
					break;
				case 432:
					rtnStr = "IRN";
					break;
				case 434:
					rtnStr = "UZB";
					break;
				case 436:
					rtnStr = "TJK";
					break;
				case 437:
					rtnStr = "KGZ";
					break;
				case 438:
					rtnStr = "TKM";
					break;
				case 440:
				case 441:
					rtnStr = "JPN";
					break;
				case 450:
					rtnStr = "KOR";
					break;
				case 452:
					rtnStr = "VNM";
					break;
				case 454:
					rtnStr = "HKG";
					break;
				case 455:
					rtnStr = "MAC";
					break;
				case 456:
					rtnStr = "KHM";
					break;
				case 457:
					rtnStr = "LAO";
					break;
				case 460:
				case 461:
					rtnStr = "CHN";
					break;
				case 466:
					rtnStr = "TWN";
					break;
				case 467:
					rtnStr = "PRK";
					break;
				case 470:
					rtnStr = "BGD";
					break;
				case 472:
					rtnStr = "MDV";
					break;
				case 502:
					rtnStr = "MYS";
					break;
				case 505:
					rtnStr = "AUS";
					break;
				case 510:
					rtnStr = "IDN";
					break;
				case 514:
					rtnStr = "TMP";
					break;
				case 515:
					rtnStr = "PHL";
					break;
				case 520:
					rtnStr = "THA";
					break;
				case 525:
					rtnStr = "SGP";
					break;
				case 528:
					rtnStr = "BRN";
					break;
				case 530:
					rtnStr = "NZL";
					break;
				case 534:
					rtnStr = "MNP";
					break;
				case 535:
					rtnStr = "GUM";
					break;
				case 536:
					rtnStr = "NRU";
					break;
				case 537:
					rtnStr = "PNG";
					break;
				case 539:
					rtnStr = "TON";
					break;
				case 540:
					rtnStr = "SLB";
					break;
				case 541:
					rtnStr = "VUT";
					break;
				case 542:
					rtnStr = "FJI";
					break;
				case 543:
					rtnStr = "WLF";
					break;
				case 544:
					rtnStr = "ASM";
					break;
				case 545:
					rtnStr = "KIR";
					break;
				case 546:
					rtnStr = "NCL";
					break;
				case 547:
					rtnStr = "PYF";
					break;
				case 548:
					rtnStr = "COK";
					break;
				case 549:
					rtnStr = "WSM";
					break;
				case 550:
					rtnStr = "FSM";
					break;
				case 551:
					rtnStr = "MHL";
					break;
				case 552:
					rtnStr = "PLW";
					break;
				case 602:
					rtnStr = "EGY";
					break;
				case 603:
					rtnStr = "DZA";
					break;
				case 604:
					rtnStr = "MAR";
					break;
				case 605:
					rtnStr = "TUN";
					break;
				case 606:
					rtnStr = "LBY";
					break;
				case 607:
					rtnStr = "GMB";
					break;
				case 608:
					rtnStr = "SEN";
					break;
				case 609:
					rtnStr = "MRT";
					break;
				case 610:
					rtnStr = "MLI";
					break;
				case 611:
					rtnStr = "GIN";
					break;
				case 612:
					rtnStr = "CIV";
					break;
				case 613:
					rtnStr = "BFA";
					break;
				case 614:
					rtnStr = "NER";
					break;
				case 615:
					rtnStr = "TGO";
					break;
				case 616:
					rtnStr = "BEN";
					break;
				case 617:
					rtnStr = "MUS";
					break;
				case 618:
					rtnStr = "LBR";
					break;
				case 619:
					rtnStr = "SLE";
					break;
				case 620:
					rtnStr = "GHA";
					break;
				case 621:
					rtnStr = "NGA";
					break;
				case 622:
					rtnStr = "TCD";
					break;
				case 623:
					rtnStr = "CAF";
					break;
				case 624:
					rtnStr = "CMR";
					break;
				case 625:
					rtnStr = "CPV";
					break;
				case 626:
					rtnStr = "STP";
					break;
				case 627:
					rtnStr = "GNQ";
					break;
				case 628:
					rtnStr = "GAB";
					break;
				case 629:
					rtnStr = "COG";
					break;
				case 630:
					rtnStr = "COD";
					break;
				case 631:
					rtnStr = "AGO";
					break;
				case 632:
					rtnStr = "GNB";
					break;
				case 633:
					rtnStr = "SYC";
					break;
				case 634:
					rtnStr = "SDN";
					break;
				case 635:
					rtnStr = "RWA";
					break;
				case 636:
					rtnStr = "ETH";
					break;
				case 637:
					rtnStr = "SOM";
					break;
				case 638:
					rtnStr = "DJI";
					break;
				case 639:
					rtnStr = "KEN";
					break;
				case 640:
					rtnStr = "TZA";
					break;
				case 641:
					rtnStr = "UGA";
					break;
				case 642:
					rtnStr = "BDI";
					break;
				case 643:
					rtnStr = "MOZ";
					break;
				case 645:
					rtnStr = "ZMB";
					break;
				case 646:
					rtnStr = "MDG";
					break;
				case 647:
					rtnStr = "REU";
					break;
				case 648:
					rtnStr = "ZWE";
					break;
				case 649:
					rtnStr = "NAM";
					break;
				case 650:
					rtnStr = "MWI";
					break;
				case 651:
					rtnStr = "LSO";
					break;
				case 652:
					rtnStr = "BWA";
					break;
				case 653:
					rtnStr = "SWZ";
					break;
				case 654:
					rtnStr = "COM";
					break;
				case 655:
					rtnStr = "ZAF";
					break;
				case 657:
					rtnStr = "ERI";
					break;
				case 702:
					rtnStr = "BLZ";
					break;
				case 704:
					rtnStr = "GTM";
					break;
				case 706:
					rtnStr = "SLV";
					break;
				case 708:
					rtnStr = "HND";
					break;
				case 710:
					rtnStr = "NIC";
					break;
				case 712:
					rtnStr = "CRI";
					break;
				case 714:
					rtnStr = "PAN";
					break;
				case 716:
					rtnStr = "PER";
					break;
				case 722:
					rtnStr = "ARG";
					break;
				case 724:
					rtnStr = "BRA";
					break;
				case 730:
					rtnStr = "CHL";
					break;
				case 732:
					rtnStr = "COL";
					break;
				case 734:
					rtnStr = "VEN";
					break;
				case 736:
					rtnStr = "BOL";
					break;
				case 738:
					rtnStr = "GUY";
					break;
				case 740:
					rtnStr = "ECU";
					break;
				case 742:
					rtnStr = "GUF";
					break;
				case 744:
					rtnStr = "PRY";
					break;
				case 746:
					rtnStr = "SUR";
					break;
				case 748:
					rtnStr = "URY";
					break;
				case 750:
					rtnStr = "FK";
					break;
				default:
					rtnStr = "";
					break;
			}
		}

		return rtnStr;
	}

	static String[] SplitString(String inString, String seperator)
	{
		Vector nodes = new Vector();
		int i;

		i = inString.indexOf(seperator);

		while (i > 0)
		{
			nodes.addElement(inString.substring(0, i));
			inString = inString.substring(i + seperator.length());
			i = inString.indexOf(seperator);
		}
		nodes.addElement(inString);

		String[] rtnArray = new String[nodes.size()];

		if (nodes.size() > 0)
		{
			for (int loop = 0; loop < nodes.size(); loop++)
			{
				rtnArray[loop] = (String) nodes.elementAt(loop);
			}
		}
		return rtnArray;
	}

	static String PadLeft(String strToPad, char charPadding, int intLength)
	{
		while (strToPad.length() < intLength)
		{
			strToPad = charPadding + strToPad;
		}
		return strToPad;
	}

	static String PadRight(String strToPad, char charPadding, int intLength)
	{
		while (strToPad.length() < intLength)
		{
			strToPad = strToPad + charPadding;
		}
		return strToPad;
	}

}
